package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;

/**
 * 菜单控制类
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenuByUserName.do")
    public Result findMenuByUserName(){
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            List<Menu> menuList = menuService.findMenuByUserName(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findAllParent")
    public Result findAllParent(){
        try{
            Set<Menu> menus = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menus);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        Set<Menu> menus = menuService.findAll();
        return new Result(true,null,menus);
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());

        return pageResult;

    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){

        try{

            /*验证菜单名称是否存在*/
            Long nameCount = menuService.findByName(menu.getName());
            if (nameCount > 0){
                /*菜单名已存在 返回提示*/
                return new Result(false,"菜单名称已存在，请修改后再尝试");
            }

            Integer priority= menu.getPriority();
            Integer parentMenuId = menu.getParentMenuId();

            /*封装 优先级和菜单等级数据*/
            Map<String,Object> pathAndPriorityMap = new HashMap<>();
            pathAndPriorityMap.put("priority",priority);
            pathAndPriorityMap.put("parentMenuId",parentMenuId);

            /*验证优先级是否存在*/
            Integer priorityCount = menuService.findByPriority(pathAndPriorityMap);
            if (priorityCount > 0){
                /*菜单已存在 返回提示*/
                return new Result(false,"菜单优先级已存在，请修改后再尝试");
            }

            /*验证是父菜单还是子菜单*/
            if (parentMenuId ==null){

                /*设置新菜单路径*/
                Integer pathNum = Integer.parseInt(menuService.findMaxPath());
                menu.setPath(String.valueOf(pathNum+1));

            }else {

                /*子菜单 设置子菜单路径*/
                String path = menu.getPath();
                menu.setPath("/"+path+"-"+priority);

            }
            menuService.add(menu);

            return new Result(true,"新增菜单成功！");

        }catch(Exception e){

            e.printStackTrace();
            return new Result(false, "新增菜单异常！");

        }

    }

    /**
     * 通过id查询菜单信息
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){

        try{

            Menu menu = menuService.findById(id);
            return new Result(true,"查询菜单信息成功",menu);

        }catch(Exception e){

            e.printStackTrace();
            return new Result(false, "查询菜单信息异常");

        }

    }

    /**
     * 查询父类菜单信息
     * @return
     */
    @RequestMapping("findAllParentMenu")
    public Result findAllParentMenu(){

        try{

            List<Menu> list = menuService.findAllParentMenu();
            return new Result(true,"查询父类菜单信息成功",list);

        }catch(Exception e){

            e.printStackTrace();
            return new Result(false, "查询父类菜单信息异常");

        }

    }

    /**
     * 通过id查询父类菜单路径信息
     * @param id
     * @return
     */
    @RequestMapping("findPathById")
    public Result findPathById(Integer id){

        try{

            String path = menuService.findPathById(id);
            return new Result(true,"查询父类菜单路径成功",path);

        }catch(Exception e){

            e.printStackTrace();
            return new Result(false,"查询父类菜单路径异常");

        }

    }

    /**
     * 编辑菜单
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu){

        try{

            Result result = menuService.edit(menu);

            return result;

        }catch(Exception e){

            e.printStackTrace();
            return new Result(false,"更新菜单信息异常");

        }

    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(Integer id){

        try{

            Result result = menuService.delete(id);
            return result;

        }catch(Exception e){
            return new Result(false,"删除菜单异常,请注意是否有子菜单！");
        }

    }

}
